package com.tanhua.listener;

import com.tanhua.api.mongo.PublishApi;
import com.tanhua.mongo.Publish;
import com.tanhua.template.HuaWeiUGCTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-group")
@Component
public class HuaWeiListener implements RocketMQListener<String> {
    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {

        System.out.println("我开始审核了");
        Publish publish = publishApi.findPublishById(publishId);
        String textContent = publish.getTextContent();
        System.out.println(textContent);
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        String[] url = publish.getMedias().toArray(new String[]{});
        if (url != null && textContent != null) {
            flag1 = huaWeiUGCTemplate.textContentCheck(textContent);
            flag2 = huaWeiUGCTemplate.imageContentCheck(url);
            flag3 = true;
        }
        int state;
        if (flag3){
            if (flag1 && flag2) {
                state = 1; //审核通过
            }else {
                state = 2;
            }
            //修改
            publishApi.updateState(publishId, state);
        }

    }
}
